(function ($) {
/**
 * 表格查询
 * @param cols
 * @param rows
 */
$.fn.table=function (cols,rows) {
    var that=this;

    Drawtable(that,cols,rows);

}
/**
 * 表格分页插件
 * @param cols
 * @param pagecount
 */
$.fn.tablepage=function (cols,pagecount) {
    var that=this;
    var url=$(that).attr("data-url");
    if(!url){
        layer.msg("请配置数据请求链接data-url地址！");
        return;
    }
    $.post(url,{currentPage:1,showCount:pagecount},function (data) {
        Drawtable(that,data.cols,data.rows);
    });
    // Drawtable(that,cols,rows)
    var pageinfo=$("<div></div>").css({"float":"left","margin": "10px 0px","margin-left": "10px"});
    var layerpage=$("<div></div>").css({"float":"right","margin-right": "1px"});
    var page=$("<div></div>").append(pageinfo).append(layerpage);
    $(this).after(page);
    laypage({
        cont: layerpage, //分页容器的id
        pages: 11, //总页数
        skip: true,
        first: 1, //将首页显示为数字1,。若不显示，设置false即可
        last: 11, //将尾页显示为总页数。若不显示，设置false即可
        prev: '<', //若不显示，设置false即可
        next: '>' //若不显示，设置false即可
        ,jump: function(obj, first){
            pageinfo.html("共"+count+"条记录,共"+obj.curr+"页,当前第"+obj.pages+"页");
            if(!first){
                $.post(url,{currentPage:obj.curr,showCount:pagecount},function (data) {
                    Drawtable(that,data.cols,data.rows);
                });
            }


        }
    });
}
/**
 * 表格绘制
 * that 表格对象本身this
 * cols 列设置
 * rows 行数据
 * */
function Drawtable(that,cols,rows){
    $(that).addClass("table table-bordered");
    $(that).css({"margin-bottom":"10px"});
    var htrdom=$("<tr></tr>");
    var checkalldom=$("<input type='checkbox'>").bind("change",function(){
        $(this).parents('tr').siblings().find('input.rowcheck').prop("checked",$(this).is(':checked'));
    });

    $("<th></th>").css({"width":"50px","text-align":"center"}).append(checkalldom).appendTo(htrdom)
    cols.forEach(function(col, index, array) {
        $("<th></th>").css({"width":col.width}).html(col.name).appendTo(htrdom);
    });

    var tbodydom=$("<tbody></tbody>").append(htrdom);
    rows.forEach(function (row,index,array) {
        var tbodytrdom=$("<tr></tr>")
        tbodytrdom.data("rowdata",row);
        $("<td></td>").css({"width":"50px","text-align":"center"}).append("<input type='checkbox' class='minimal rowcheck'>").appendTo(tbodytrdom);
        cols.forEach(function(col, index, array) {
            var value=row[col.field]
            if(col.render){
                var func=eval(col.render);
                var e={};
                e.value=value;
                e.index=index;
                e.row=row;
                func(e);
                value=e.value;
            }
            $("<td></td>").html(value).appendTo(tbodytrdom);
        });
        tbodydom.append(tbodytrdom);
    })
    $(that).html("");
    $(that).append(tbodydom);
}
})(jQuery)